package org.kitten.core.util;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 解决Log4J只能记录简单错误的工具类.<br>
 * 这个类中的方法配合log4j能够打印出详细的堆栈信息.<br>
 * <br>
 * 使用方法:<br>
 * 
 * <pre>
 *  ...
 *  } catch(Throwable e){
 *  	logger.error(ErrorUtil.getError(e));
 *  }
 * </pre>
 * 
 * @author cyper
 * @version 1.0
 */
public class ErrorUtil {

	/**
	 * 取得堆栈的详细信息.
	 * @param e
	 * @return
	 */
	public static String getError(Throwable e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return sw.toString();
	}
	public static String getError(Throwable e,int maxLength) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		String str = sw.toString();
		if (str!=null && str.length()>maxLength) {
			str = str.substring(0,maxLength);
		}
		return str;
	}
}